//
//  TFGuideInterfaceView.m
//  引导界面
//
//  Created by sunrise-OYR on 16/10/24.
//  Copyright (c) 2016年 Liwy. All rights reserved.
//

#import "BDGuideInterfaceView.h"

#define TF_GUIDE_INTERFACE_IMAGEDATA_COUNT _imageDataArray.count

@interface BDGuideInterfaceView () <UIScrollViewDelegate> {
    NSMutableArray *_imageDataArray;
}

@property (nonatomic, strong)UIButton *enterButton;           // 进入主页按钮
@property (nonatomic, strong)UIScrollView *scrollView;        // 滚动视图
@property (nonatomic, strong)UIPageControl *pageControl;      // 分页指示器

@end

@implementation BDGuideInterfaceView

- (instancetype)initWithFrame:(CGRect)frame imageDataArray:(NSArray *)imageDataArray {
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeDatasourceWithImaegDataArray:imageDataArray];
        [self initializeUserInterface];
    }
    return self;
}

- (void)initializeDatasourceWithImaegDataArray:(NSArray *)imageDataArray {
    _imageDataArray = [[NSMutableArray alloc] initWithArray:imageDataArray];
}

- (void)initializeUserInterface {
    
    // 背景视图
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mScreen_W, mScreen_H)];
    backGroundView.backgroundColor = [UIColor blackColor];
    
    [self addSubview:backGroundView];
    
    self.scrollView = ({
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, mScreen_W, mScreen_H)];
        scrollView.contentSize = CGSizeMake(TF_GUIDE_INTERFACE_IMAGEDATA_COUNT * mScreen_W, mScreen_H);
        scrollView.contentOffset = CGPointZero;
        scrollView.delegate = self;
        scrollView.pagingEnabled = YES;
        
        [backGroundView addSubview:scrollView];
        
        scrollView;
    });
    
    // 添加图片视图
    for (int i = 0; i < TF_GUIDE_INTERFACE_IMAGEDATA_COUNT; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * mScreen_W, 0, mScreen_W, mScreen_H)];
//        imageView.image = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:_imageDataArray[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        imageView.image = [UIImage imageNamed:_imageDataArray[i]];
        
        [_scrollView addSubview:imageView];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * mScreen_W, mScreen_H * 0.743, mScreen_W, 50);
//        button.backgroundColor = [UIColor greenColor];
        [button addTarget:self action:@selector(enterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:button];
        
    }
    
    // 按钮
//    self.enterButton = ({
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.bounds = CGRectMake(0, 0, SCREEN_W / 2, 50);
//        button.center = CGPointMake((TF_GUIDE_INTERFACE_IMAGEDATA_COUNT - 1)*SCREEN_W + SCREEN_W / 2, SCREEN_H - 50 * HRadio);
//        
//        [button addTarget:self action:@selector(enterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//        [_scrollView addSubview:button];
//        
//        button;
//    });
    
//    self.pageControl = ({
//        UIPageControl *pageControl = [[UIPageControl alloc] init];
//        pageControl.bounds = CGRectMake(0, 0, SCREEN_W, 35);
//        pageControl.center = CGPointMake(SCREEN_W / 2, SCREEN_H - 120);
//        pageControl.numberOfPages = TF_GUIDE_INTERFACE_IMAGEDATA_COUNT;
//        pageControl.pageIndicatorTintColor = [UIColor darkGrayColor];
//        pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
//        
//        [self addSubview:pageControl];
//        
//        pageControl;
//    });
}

- (void)enterButtonPressed:(UIButton *)sender {
    
    NSInteger index = _scrollView.contentOffset.x / mScreen_W;
    if (index < TF_GUIDE_INTERFACE_IMAGEDATA_COUNT - 1) {
        [_scrollView setContentOffset:CGPointMake(mScreen_W * (index + 1), 0) animated:YES];
        return;
    }
    
    [UIView animateWithDuration:0.4 animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.alpha = 0.3;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _pageControl.currentPage = scrollView.contentOffset.x / mScreen_W;
}


@end





































